package com.study.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.resource.HttpResource;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.study.model.pojo.Dept;
import com.study.services.CollectionService;
import com.study.services.CusmshfitServices;
import com.study.services.CustomerService;
import com.study.services.DeptService;
import com.study.services.PermissionServices;
import com.study.services.RoleService;
import com.study.services.UserService;

@Controller
public class PermissionController {
	@Autowired
	UserService uService;
	@Autowired
	RoleService roleService;
	@Autowired
	PermissionServices permService;
	// 定义公共变量用来保存选中的权限id
	int xm;
	String urls;

	/*
	 * @Autowired DeptService deptService;
	 * 
	 * @Autowired CusmshfitServices cusms;
	 * 
	 * @Autowired CustomerService cust; String[] xms= {};
	 */
	/**
	 * 实现跳转到权限设置页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/perm")
	public ModelAndView getPermsAll() {
		// List<JSONObject> findDeptAll = deptService.getFindDeptAll();
		ModelAndView mv = new ModelAndView("/permission/userpermission");
		// dept.getfindse();

		return mv;
	}

	/**
	 * 弹出销售权限
	 * 
	 * @param linid
	 * @return
	 */
	@GetMapping("customer/adduserpermissxiaoshu")
	public ModelAndView adduserxiao() {
		ModelAndView mv = new ModelAndView("/permission/adduserpermissxiaoshu");
		System.out.println("232");
		xm = 2;
		List<JSONObject> findUserAll = uService.getRolidUser(xm);
		List<JSONObject> obj = new ArrayList<>();
		for (JSONObject jsonObject : findUserAll) {
			JSONObject objse = new JSONObject();
			System.out.println(jsonObject + "         销售权限");
			objse.put("label", jsonObject.getString("use_name"));
			objse.put("value", jsonObject.getString("use_id"));
			obj.add(objse);
		}
		mv.addObject("findUser", obj);
		return mv;
	}

	/**
	 * 弹出产品权限
	 * 
	 * @param linid
	 * @return
	 */
	@GetMapping("customer/adduserpermissgoods")
	public ModelAndView adduserchan() {
		ModelAndView mv = new ModelAndView("/permission/adduserpermissgoods");
		System.out.println("232");
		xm = 3;
		List<JSONObject> findUserAll = uService.getRolidUser(xm);
		List<JSONObject> obj = new ArrayList<>();
		for (JSONObject jsonObject : findUserAll) {
			JSONObject objse = new JSONObject();
			System.out.println(jsonObject + "         产品权限");
			objse.put("label", jsonObject.getString("use_name"));
			objse.put("value", jsonObject.getString("use_id"));
			obj.add(objse);
		}
		mv.addObject("findUser", obj);
		return mv;
	}

	/**
	 * 弹出维修权限
	 * 
	 * @param linid
	 * @return
	 */
	@GetMapping("customer/addusermissionweixiu")
	public ModelAndView adduserwei() {
		ModelAndView mv = new ModelAndView("/permission/addusermissionweixiu");
		System.out.println("232");
		xm = 4;
		List<JSONObject> findUserAll = uService.getRolidUser(xm);
		List<JSONObject> obj = new ArrayList<>();
		for (JSONObject jsonObject : findUserAll) {
			JSONObject objse = new JSONObject();
			System.out.println(jsonObject + "          维修权限");
			objse.put("label", jsonObject.getString("use_name"));
			objse.put("value", jsonObject.getString("use_id"));
			obj.add(objse);
		}
		mv.addObject("findUser", obj);
		return mv;
	}

	/**
	 * 弹出客服权限
	 * 
	 * @param linid
	 * @return
	 */
	@GetMapping("customer/adduserpermisskefu")
	public ModelAndView adduserkefu() {
		ModelAndView mv = new ModelAndView("/permission/adduserpermisskefu");
		xm = 5;
		List<JSONObject> findUserAll = uService.getRolidUser(xm);
		List<JSONObject> obj = new ArrayList<>();
		for (JSONObject jsonObject : findUserAll) {
			JSONObject objse = new JSONObject();
			System.out.println(jsonObject + "         客服权限");
			objse.put("label", jsonObject.getString("use_name"));
			objse.put("value", jsonObject.getString("use_id"));
			obj.add(objse);
		}
		mv.addObject("findUser", obj);
		return mv;
	}

	/**
	 * 或取要被设置权限的用户id 给用户设置权限对应的角色
	 * 
	 * @param array
	 */
	@RequestMapping(value = "/selsepsersreon", method = { RequestMethod.POST })
	public void getkehufenxiang(@RequestParam("value5[]") String[] value5,@RequestParam("roleid") String roleid) {		
		//根据角色删除对应的角色
		permService.setdelectRolepersstion(roleid);
		for (String string : value5) {
			//System.out.println(string);
			//实现角色和权限的绑定
			roleService.setrolePersstion(roleid, string);
		}
		/*String string = null;
		int se = 0;
		System.out.println(xm + "     xmsds");
		List<JSONObject> findRole = permService.getFindRole(xm);
		for (JSONObject jsonObject : findRole) {
			string = jsonObject.getString("rol_id");
			System.out.println(string + "            struing");
		}
		for (String usrid : value5) {
			se = uService.setUserRole(Integer.parseInt(usrid), Integer.parseInt(string));
		}*/
	}

	@PostMapping("/getfindmenu")
	@ResponseBody
	public JSONArray getFindMenu(HttpServletRequest request) {
		String[] str = { "百度ECharts", "集客", "获客线索", "个人资料", "联系人", "纪念日", "客户合并", "客户转移/共享", "客户", "销售机会", "销售漏斗",
				"客户需求", "解决方案", "竞争对手", "报价单", "报价明细", "订单", "订单明细", "发货单", "退货", "退货明细", "客户服务", "客户投诉", "客户关怀",
				"维修总控台", "维修工单", "分类", "产品", "部门和用户设置", "角色和权限设置" };
		String[] url = { "/getChartty", "/getCustomergx", "/getCustomerty", "/getCustomerwcx", "/getLinkmanwcx",
				"/getAnniversarylsl", "/getHebingwjl", "/getZhuanyiwjl", "/getGongxiangiwjl", "/getJihuiwcx",
				"/getLouduwcx", "/getXuqiuwcx", "/getFanganwcx", "/getDuiShouwcx", "/getBaojiawcx", "/getBaojiaMorewcx",
				"/getOrdergx", "/getOrderMoregx", "/getFahuogx", "/getBackgx", "/getBackMoregx", "/getServicety",
				"/getTousuty", "/getCarety", "/getWeixiuty", "/goods/category", "/getCatlsl", "/getGoodlsl", "/dept",
				"/userpermission" };
		List<String> roleid = (List<String>) request.getSession().getAttribute("roleid");
		JSONArray arry = new JSONArray();
		for (String string : roleid) {
			if (string.equals("4")) {
				for (int i = 0; i <= str.length - 1; i++) {
					if (str[i].equals("维修总控台") || str[i].equals("维修工单")) {
						JSONObject obj = new JSONObject();
						obj.put("value", str[i]);
						obj.put("urladss", url[i]);
						arry.add(obj);
					}
				}
			} else if (string.equals("5")) {
				for (int i = 0; i <= str.length - 1; i++) {
					if (str[i].equals("客户服务") || str[i].equals("客户投诉") || str[i].equals("客户关怀")) {
						JSONObject obj = new JSONObject();
						obj.put("value", str[i]);
						obj.put("urladss", url[i]);
						arry.add(obj);
					}
				}
			} else if (string.equals("3")) {
				for (int i = 0; i <= str.length - 1; i++) {
					if (str[i].equals("分类") || str[i].equals("产品")) {
						JSONObject obj = new JSONObject();
						obj.put("value", str[i]);
						obj.put("urladss", url[i]);
						arry.add(obj);
					}
				}
			} else if (string.equals("2")) {
				for (int i = 0; i <= str.length - 1; i++) {
					if (!str[i].equals("分类") && !str[i].equals("产品") && !str[i].equals("客户服务") && !str[i].equals("客户投诉")
							&& !str[i].equals("客户关怀") && !str[i].equals("维修总控台") && !str[i].equals("维修工单")) {
						JSONObject obj = new JSONObject();
						obj.put("value", str[i]);
						obj.put("urladss", url[i]);
						arry.add(obj);
					}
				}
			} else {
				for (int i = 0; i <= str.length - 1; i++) {
					JSONObject obj = new JSONObject();
					obj.put("value", str[i]);
					obj.put("urladss", url[i]);
					arry.add(obj);
				}
			}
		}
		System.out.println(arry.toString());
		return arry;
	}

	/**
	 * 获取搜索的网址
	 * 
	 * @param url
	 * @return
	 * @return
	 */
	@PostMapping("/seturlse")
	/* @ResponseBody */
	public ModelAndView setUrlse(@RequestParam("urladss") String url) {
		System.out.println(url);
		urls = url;
		ModelAndView mv = new ModelAndView(url);
		return mv;
	}

	@GetMapping("finduer")
	@ResponseBody
	public ModelAndView finduer() {
		ModelAndView mv = new ModelAndView(urls);
		return mv;
	}
	/**
	 * 根据角色id查询对应的权限
	 * @param rolid
	 * @return 
	 */
	@GetMapping("/getpersstion")
	public ModelAndView getFindPersstionrole(@RequestParam("rolid") String rolid) {
		List<JSONObject> getfidnPerstionroleid = permService.getfidnPerstionroleid(rolid);
		ModelAndView mv=new ModelAndView("/permission/adduserpermissxiaoshu");
		List<JSONObject> obj = new ArrayList<>();
		for (JSONObject jsonObject : getfidnPerstionroleid) {
			JSONObject objse = new JSONObject();
			objse.put("label", jsonObject.getString("per_name"));
			objse.put("value", jsonObject.getString("per_id"));
			obj.add(objse);
		}
		mv.addObject("roidsf", rolid);
		mv.addObject("persst", obj);
	
		return mv;
	}
	
}
